package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.LkRoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author likun
 * @version V1.0
 * @date 2020/3/10 10:37
 */
@RestController
@RequestMapping("/role")
public class LkRoleController {
    @Reference
    private LkRoleService roleService;
    /**
     * 查询所有角色表的数据返回List<Role>
     * @return
     */
    @RequestMapping("/getAllRole")
    public Result findAllRole(){
        try{

            List<Role> allRole = roleService.findAllRole();
            return new Result(true,MessageConstant.QUERY_ROLE_LIST_SUCCESS,allRole);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_LIST_FAIL);
        }

    }

    /**
     * 根据角色id修改对应的权限
     * @param currentId
     * @param rolePermissionIds
     * @return
     */
    @RequestMapping("/updatePermission4Role")
    public Result updatePermission4Role(Integer currentId, @RequestBody Integer[] rolePermissionIds){
        try{
            for (Integer rolePermissionId : rolePermissionIds) {

                System.out.println(rolePermissionId);
            }

            roleService.updatePermission4Role(currentId,rolePermissionIds);
            return new Result(true,MessageConstant.UPDATE_PERMISSION_SUCCESS);
        } catch (Exception e){

            return new Result(false,MessageConstant.UPDATE_PERMISSION_FAIL);
        }
    }

    /**
     * 添加角色信息
     * @param role
     * @param rolePermissionIds
     * @param roleMenuIds
     * @return
     */
    @RequestMapping("/addRole")
    public Result addRole(@RequestBody Role role,Integer[] rolePermissionIds,Integer[] roleMenuIds){
        try {

            roleService.addRole(role,rolePermissionIds,roleMenuIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e){
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }

    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return roleService.findPage(pageBean);
    }
    @RequestMapping("/delRole")
    public Result delRole(Integer id){
        try{
            roleService.deleteRole(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e){
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
